#encoding: utf-8

from .exts import db

class User(db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    uname = db.Column(db.String(100), nullable=False)
    upwd = db.Column(db.String(100), nullable=False)
    uphnum = db.Column(db.String(100), nullable=False)
    balance = db.Column(db.Numeric(10,2),default=0.00)
    nickname = db.Column(db.String(100),nullable=True)
    trans_pwd = db.Column(db.String(100),nullable=False)

    def to_dict(self):
        dic = {
            "id" : self.id,
            "uname" : self.uname,
            "upwd" : self.upwd,
            "uphnum" : self.uphnum,
            "balance" : self.balance,
            "nickname" : self.nickname,
            "trans_pwd" : self.trans_pwd
        }
        return dic

# 全国的省市区／县的实体类--省
class Province(db.Model):
    __tablename__ = "province"
    id = db.Column(db.Integer,primary_key=True)
    pname = db.Column(db.String(30))
    cities = db.relationship('City',backref='province',lazy='dynamic')

    def __init__(self,id,pname):
        self.id = id
        self.pname = pname

    def to_dic(self):
        dic = {
            'id':self.id,
            'pname':self.pname
        }
        return dic
# 全国的省市区／县的实体类--市    
class City(db.Model):
    __tablename__ = 'city'
    id = db.Column(db.Integer,primary_key=True)
    cname = db.Column(db.String(30))
    pid = db.Column(db.Integer,db.ForeignKey('province.id'),nullable=False)    
    regions = db.relationship('Region',backref='city',lazy='dynamic')
    
    def __init__(self,id,cname,pid):
        self.id = id
        self.cname = cname
        self.pid = pid

    def to_dic(self):
        dic = {
            'id':self.id,
            'cname':self.cname,
            'pid':self.pid
        }
        return dic
# 全国的省市区／县的实体类--区/县
class Region(db.Model):
    __tablename__ = 'region'
    id = db.Column(db.Integer,primary_key=True)
    rname = db.Column(db.String(30))
    cid = db.Column(db.Integer,db.ForeignKey('city.id'),nullable=False)    
    
    def __init__(self,id,rname,cid):
        self.id = id
        self.rname = rname
        self.cid = cid

    def to_dic(self):
        dic = {
            'id':self.id,
            'rname':self.rname,
            'cid':self.cid
        }
        return dic         